<?php

class PacotesController extends Site_Controller_Action {

    public $limitRegistros = 3;

    public function init() {
        /* Initialize action controller here */
    }

    public function indexAction() {
       
        Zend_Loader::loadClass('datetools','library');
        
        $this->view->headLink()->prependStylesheet($this->_urlbase . '/public/media/js/jquery-ui-1.10.2.custom/css/custom-theme/jquery-ui-1.10.2.custom.css');
        $this->view->headScript()->appendFile($this->_urlbase . '/public/media/js/jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js');

        $this->view->headScript()->appendFile($this->_urlbase . '/public/media/js/jquery.maskMoney.min.js');
        $this->view->headScript()->appendFile($this->_urlbase . '/public/media/js/jquery.maskedinput.min.js');
        $this->view->headScript()->appendFile($this->_urlbase . '/public/media/js/mask.js');

        $this->view->headScript()->appendFile($this->_urlbase . '/public/media/js/validation/dist/jquery.validate.min.js');
        $this->view->headScript()->appendFile($this->_urlbase . '/public/media/js/view/pacotes.js?1=2');

        $query = 'SELECT SQL_CACHE * FROM PACOTE WHERE PCT_STA = 1 ORDER BY PCT_ORD ASC LIMIT 3';
        $result = $this->db->fetchAll($query);

        $query = 'SELECT SQL_CACHE count(*) as TOTAL FROM PACOTE WHERE PCT_STA = 1';
        $this->view->total = $this->db->fetchRow($query);

        $this->view->limitRegistros = $this->limitRegistros;

        $this->view->result = $result;
        
    }

    public function maispacotesAction() {
        $this->_helper->layout->disableLayout();
        $this->_helper->viewRenderer->setNoRender();

        $offset = (int) $this->_getParam('offset', 0) + $this->limitRegistros;

        $query = 'SELECT SQL_CACHE * FROM PACOTE WHERE PCT_ID > ' . $offset . ' AND PCT_STA = 1 ORDER BY PCT_ORD ASC LIMIT ' . $this->limitRegistros;
        $result = $this->db->fetchAll($query);

        $count = 0;
        $btn_mais = 1;
        foreach ($result as $res)
            $count += 1;
        if ($count == 0) {
            $offset = $offset - $this->limitRegistros;
            $btn_mais = 0;
        } else if ($count < $this->limitRegistros) {
            $btn_mais = 0;
        }

        $html = $this->view->partial('pacotes/list-index.phtml', array('urlbase' => $this->_urlbase, 'result' => $result));

        echo json_encode(array('ok' => true, 'html' => $html, 'offset' => $offset, 'btn_mais' => $btn_mais));
    }
    
    public function interesseAction()
    {
        $this->_helper->layout->disableLayout(); 
        $this->_helper->viewRenderer->setNoRender();  
        $ok = FALSE;
        $msg = '';
        try {
            if($this->getRequest()->isPost()) {
                
                $formData = $this->getRequest()->getPost();

                $data = array(
                    'PCT_ID' => $formData['cod-pacote'],
                    'INT_NOM' => $formData['nome'],
                    'INT_TEL' => $formData['telefone'],
                    'INT_EML' => $formData['email'],
                    'INT_CID' => $formData['cidade'],
                    'INT_DAT' => date('Y-m-d'),
                    'INT_STA' => 1,
                );

                $int = $this->db->insert('INTERESSE', $data);
                
                $pac = $this->db->fetchRow('SELECT SQL_CACHE PCT_NOM  FROM PACOTE WHERE PCT_ID = '.$formData['cod-pacote']);
                    
                Zend_Loader::loadClass('envioemail','library');
                $arr['pacote'] = $pac['PCT_NOM'];
                $arr['nome'] = $formData['nome'];
                $arr['email'] = $formData['email'];
                $arr['telefone'] = $formData['telefone'];
                $arr['cidade'] = $formData['cidade'];
                $arr['urlbase']  = $this->_urlbase;
                $ok = envioemail::interesse($arr);
                
                $ok = true;
                
            }
        } catch (Exception $ex) {
            $msg = 'Erro ao enviar interesse!';
        }
        
        echo json_encode(array('ok' => $ok,'msg'=>$msg));
    }

    
}
